var seckill = {
    url : {
        now : function () {
            return "/seckill/time/now";
        },
        exposer : function (seckillId) {
            return "/seckill/"+seckillId+"/exposer";
        },
        execution : function (seckillId,md5) {
            return "/seckill/"+seckillId+"/"+md5+"/execution";
        }
    },

    validatePhone : function (phone) {
        if(phone && !isNaN(phone) && phone.length === 11)
        {
            return true;
        }else{
            return false;
        }
    },

    handleSeckill : function (seckillId,node) {
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        $.post(seckill.url.exposer(seckillId),{},function (result) {
            if(result && result['success']) {
                var exposer = result['data'];
                if (exposer['exposed'])
                {
                    //开始
                    var md5 = exposer['md5'];

                    var killUrl = seckill.url.execution(seckillId,md5);

                    console.log("killUrl: "+killUrl);

                    $('#killBtn').one('click',function () {
                        $(this).addClass('disabled');

                        $.post(killUrl,{},function (result) {
                            if(result && result['success'])
                            {
                                var killResult = result['data'];
                                var state = killResult['state'];
                                var stateInfo = killResult['stateInfo'];
                                node.html('<span class="label label-success">'+stateInfo+'</span>');
                            }

                        });
                    });
                    node.show();
                }
                else{
                    //未开始
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    seckill.countDown(seckillId,now,start,end);
                }
            }else{
                console.log(result);

            }
        });
    },

    countDown : function (seckillId,nowTime,startTime,endTime) {
        var seckillBox = $('#seckill-box');
        if(nowTime > endTime)
        {
            seckillBox.html("秒杀结束");
        }
        else if(nowTime < startTime)
        {
            seckillBox.html("秒杀未开始");

            var killTime = new Date(startTime + 1000);

            seckillBox.countdown(killTime,function (event) {
                var format = event.strftime('秒杀倒计时:%D天 %H时 %M分 %S秒');
                seckillBox.html(format);
            }).on('finish.countdown',function () {
                seckill.handleSeckill(seckillId,seckillBox);
            });
        }else {
            seckill.handleSeckill(seckillId,seckillBox);
        }
    },
    detail : {
        init : function (params) {
            var killPhone = $.cookie('killPhone');
            var startTime = params['startTime'];
            var endTime = params['endTime'];
            var seckillId = params['seckillId'];

            if(!seckill.validatePhone(killPhone))
            {
                var killPhoneModal = $("#killPhoneModal");
                killPhoneModal.modal({
                    show : true,
                    backdrop : 'static',
                    keyboard : false
                });

                $('#killPhoneBtn').click(function () {
                    var inputPhone = $("#killPhoneKey").val();
                    if(seckill.validatePhone(inputPhone))
                    {
                        $.cookie('killPhone',inputPhone,{expires:7,path:'/seckill'});
                        window.location.reload();
                    }
                    else{
                        $('#killPhoneMessage').hide().html("<label class='label label-danger'>手机号错误</label>").show(300);
                    }
                });
            }

            $.get(seckill.url.now(),{},function (result) {
                if(result && result['success'])
                {
                    var nowTime = result['data'];
                    seckill.countDown(seckillId,nowTime,startTime,endTime);
                }
            });
        }
    }
}